using Domain;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;


namespace WriteAPI.Controllers.采购计划
{
    [ApiController]
    [Route("api/[controller]")]
    public class CgddController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ISqlSugarClient _db;

        public CgddController(IMediator mediator, ISqlSugarClient db)
        {
            _mediator = mediator;
            _db = db;
        }

        //// 新增采购订单
        //[HttpPost]
        //public async Task<ActionResult<Lwp_Cgdd>> Create([FromBody] CreateCgddCommand command)
        //{
        //    var result = await _mediator.Send(command);
        //    return Ok(result);
        //}

        // 获取采购订单列表（可分页、可按关键字筛选）
        [HttpGet]
        public async Task<ActionResult> GetList([FromQuery] string? keyword = null, [FromQuery] int page = 1, [FromQuery] int pageSize = 20)
        {
            var query = _db.Queryable<Lwp_Cgdd>();
            if (!string.IsNullOrEmpty(keyword))
            {
                query = query.Where(x => x.PurchaseNumber.Contains(keyword) || x.SupplierName.Contains(keyword));
            }
            var total = await query.CountAsync();
            var list = await query.OrderBy(x => x.Id, OrderByType.Desc).ToPageListAsync(page, pageSize);
            return Ok(new { total, list });
        }

        // 获取采购订单详情
        [HttpGet("{id}")]
        public async Task<ActionResult> GetDetail(int id)
        {
            var cgdd = await _db.Queryable<Lwp_Cgdd>().Includes(x => x.CgddItems).FirstAsync(x => x.Id == id);
            if (cgdd == null) return NotFound();
            return Ok(cgdd);
        }

        //// 修改采购订单
        //[HttpPut("{id}")]
        //public async Task<ActionResult<Lwp_Cgdd>> Update(int id, [FromBody] UpdateCgddCommand command)
        //{
        //    if (id != command.Id) return BadRequest("参数不一致");
        //    var result = await _mediator.Send(command);
        //    return Ok(result);
        //}

        //// 删除采购订单
        //[HttpDelete("{id}")]
        //public async Task<ActionResult> Delete(int id)
        //{
        //    var result = await _mediator.Send(new DeleteCgddCommand { Id = id });
        //    if (!result) return NotFound();
        //    return Ok();
        //}
    }
} 